package com.wish.g04.financial.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.Jeepay;
import com.jeequan.jeepay.JeepayClient;
import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.request.PayOrderCreateRequest;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.wish.g04.common.core.utils.ip.IpUtils;
import com.wish.g04.financial.dto.PayDTO;
import com.wish.g04.financial.service.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.logging.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @Author Z-7
 * @Date 2022/6/7
 */
@Slf4j
@Service
public class IPayServiceImpl implements IPayService {

    @Override
    public PayOrderCreateResponse startPay(PayDTO payDTO) {
        JeepayClient jeepayClient = JeepayClient.getInstance(Jeepay.appId, Jeepay.apiKey, Jeepay.getApiBase());
        PayOrderCreateRequest request = new PayOrderCreateRequest();
        PayOrderCreateReqModel model = new PayOrderCreateReqModel();
        model.setMchNo(Jeepay.mchNo);                       // 商户号
        model.setAppId(jeepayClient.getAppId());            // 应用ID
        String orderNo = "WS" + System.currentTimeMillis();
        model.setMchOrderNo(orderNo);                       // 商户订单号
        model.setWayCode(payDTO.getWayCode());                          // 支付方式
        model.setAmount(payDTO.getAmount());                                // 金额，单位分
        model.setCurrency(payDTO.getCurrency());                           // 币种，目前只支持cny
        model.setClientIp(IpUtils.getHostIp());                 // 发起支付请求客户端的IP地址
        model.setSubject(payDTO.getSubject());                         // 商品标题
        model.setBody(payDTO.getBody());                            // 商品描述
        model.setNotifyUrl(payDTO.getNotifyUrl());                            // 商品描述
        //设置扩展参数
        JSONObject extParams = new JSONObject();
        if(StringUtils.isNotEmpty(payDTO.getPayDataType())) {
            extParams.put("payDataType", payDTO.getPayDataType().trim());
        }
        model.setChannelExtra(extParams.toString());                           // 商品描述

        request.setBizModel(model);
        try {
            PayOrderCreateResponse response = jeepayClient.execute(request);
            log.info("验签结果：{}", response.checkSign(Jeepay.apiKey));
            // 下单成功
            if(response.isSuccess(Jeepay.apiKey)) {
                String payOrderId = response.get().getPayOrderId();
                log.info("payOrderId：{}", payOrderId);
                log.info("mchOrderNo：{}", response.get().getMchOrderNo());
                return response;
            }else {
                log.info("下单失败：{}", orderNo);
                log.info("通道错误码：{}", response.get().getErrCode());
                log.info("通道错误信息：{}", response.get().getErrMsg());
            }
        } catch (JeepayException e) {
            log.error(e.getMessage());
        }
        return null;
    }
}
